{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tkinter\n",
    "import threading\n",
    "import datetime\n",
    "import time\n",
    "\n",
    "app = tkinter.Tk()\n",
    "#不显示标题栏\n",
    "app.overrideredirect(True)\n",
    "#半透明窗体\n",
    "app.attributes('-alpha', 0.9)\n",
    "#窗口总是在顶端显示\n",
    "app.attributes('-topmost', 1)\n",
    "#设置初始大小与位置\n",
    "app.geometry('110x25+100+100')\n",
    "labelDateTime = tkinter.Label(app)\n",
    "labelDateTime.pack(fill=tkinter.BOTH, expand=tkinter.YES)\n",
    "labelDateTime.configure(bg = 'gray')\n",
    "#变量X和Y用于记录鼠标左键按下的位置\n",
    "X = tkinter.IntVar(value=0)\n",
    "Y = tkinter.IntVar(value=0)\n",
    "#表示窗口是否可拖动的变量\n",
    "canMove = tkinter.IntVar(value=0)\n",
    "#表示是否仍在运行变量\n",
    "still = tkinter.IntVar(value=1)\n",
    "\n",
    "def onLeftButtonDown(event):\n",
    "    #开始拖动时增加透明度\n",
    "    app.attributes('-alpha', 0.4)\n",
    "    #鼠标左键按下，记录当前位置\n",
    "    X.set(event.x)\n",
    "    Y.set(event.y)\n",
    "    #标记窗口可拖动\n",
    "    canMove.set(1)\n",
    "#绑定鼠标左键按下，记录当前位置\n",
    "labelDateTime.bind('<Button-1>', onLeftButtonDown)\n",
    "\n",
    "def onLeftButtonUp(event):\n",
    "    #停止拖动时恢复透明度\n",
    "    app.attributes('-alpha', 0.9)\n",
    "    #鼠标左键释放，标记窗口不可拖动\n",
    "    canMove.set(0)\n",
    "#绑定鼠标左键释放，标记窗口不可拖动\n",
    "labelDateTime.bind('<ButtonRelease-1>', onLeftButtonUp)\n",
    "\n",
    "def onLeftButtonMove(event):\n",
    "  if canMove.get() == 0:\n",
    "     return\n",
    "  #重新计算并修改窗口的新位置\n",
    "  newX = app.winfo_x()+(event.x-X.get())\n",
    "  newY = app.winfo_y()+(event.y-Y.get())\n",
    "  g = '110x25+'+str(newX)+'+'+str(newY)\n",
    "  app.geometry(g)\n",
    "#绑定鼠标左键移动事件处理函数\n",
    "labelDateTime.bind('<B1-Motion>', onLeftButtonMove)\n",
    "\n",
    "def onRightButtonDown(event):\n",
    "  still.set(0)\n",
    "  t.join(0.2)\n",
    "  #关闭窗口\n",
    "  app.destroy()\n",
    "#绑定鼠标右键单击事件处理函数\n",
    "labelDateTime.bind('<Button-3>',onRightButtonDown)\n",
    "\n",
    "#显示当前时间的线程函数\n",
    "def nowDateTime():\n",
    "   while still.get() == 1:\n",
    "      #获取当前的日期时间\n",
    "      now = datetime.datetime.now()\n",
    "      s = str(now.year)+'-'+str(now.month)+\\\n",
    "         '-'+str(now.day)+' '\n",
    "      s = s +str(now.hour)+':'+str(now.minute)+\\\n",
    "         ':'+str(now.second)\n",
    "      #显示当前的时间\n",
    "      labelDateTime['text'] = s\n",
    "      #0.5秒刷新一次日期时间\n",
    "      time.sleep(0.5)\n",
    "#创建并启动线程\n",
    "t = threading.Thread(target=nowDateTime)\n",
    "t.daemon = True\n",
    "t.start()\n",
    "\n",
    "app.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
